package com.glink.manage.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glink.common.util.BaseCommonUtils;
import com.glink.manage.common.Constants;
import com.glink.manage.domain.BlacklistBean;
import com.glink.manage.mapper.BlacklistMapper;
import com.glink.manage.service.BlacklistService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author  qiushaoshan
 * @since 2025-01-15
 */
@Service
@Slf4j
public class BlacklistServiceImpl extends ServiceImpl<BlacklistMapper, BlacklistBean> implements BlacklistService {

    /**
     * 根据类型查询黑名单信息
     *
     * @param type 类型ID
     * @return 黑名单列表
     */
    @Override
    public List<String> findIdNumberByType(String type) {
        List<BlacklistBean> blacklistBeanList = this.lambdaQuery().eq(BlacklistBean::getType, type).list();
        if(CollectionUtils.isNotEmpty(blacklistBeanList)){
            return blacklistBeanList.stream().map(BlacklistBean::getIdNumber).distinct().collect(Collectors.toList());
        }
        return null;
    }

    /**
     * 保存黑名单
     *
     * @param idNumber 证件照
     * @param type
     * @param userName
     * @return
     */
    @Override
    @Transactional(value = "transactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean saveByIdNumberAndTypeAndUserName(String idNumber, String type, String userName, String sourceObjId,String actionType,String reason) {
        Integer count = this.lambdaQuery().eq(BlacklistBean::getIdNumber, idNumber)
                .eq(BlacklistBean::getType, type).count();
        if(Objects.nonNull(count) && count > 0){
            return true;
        }

        BlacklistBean blacklistBean = new BlacklistBean();
        blacklistBean.setObjId(BaseCommonUtils.generateUUID());
        blacklistBean.setIdNumber(idNumber);
        blacklistBean.setType(type);
        blacklistBean.setUserName(userName);
        blacklistBean.setSourceObjId(sourceObjId);
        blacklistBean.setActionType(actionType);
        blacklistBean.setReason(reason);
        return save(blacklistBean);
    }

    /**
     * 删除黑名单
     *
     * @param idNumber 证件照
     * @param type     类型
     * @return 删除状态
     */
    @Override
    @Transactional(value = "transactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean delByIdNumberAndType(String idNumber, String type) {
        Integer count = this.lambdaQuery().eq(BlacklistBean::getIdNumber, idNumber)
                .eq(BlacklistBean::getType, type).count();
        if(Objects.nonNull(count) && count > 0){
            return this.lambdaUpdate().eq(BlacklistBean::getIdNumber, idNumber)
                    .eq(BlacklistBean::getType, type).remove();
        }
        return false;
    }

    /**
     * 校验是否是黑命大
     *
     * @param idNumber 证件照
     * @param type     类型 BaseCodeConstants.CodeType_ApprovalFormType
     * @return 状态
     */
    @Override
    public boolean checkBlackListByIdNumberAndType(String idNumber, String type) {
        Integer count = this.lambdaQuery().eq(BlacklistBean::getIdNumber, idNumber)
                .eq(BlacklistBean::getType, type).count();
        return Objects.nonNull(count) && count > 0;
    }

    /**
     * 根据类型查询黑名单信息
     *
     * @param type 类型ID
     * @return 黑名单列表
     */
    @Override
    public List<String> findSourceIdListByType(String type) {
        List<BlacklistBean> blacklistBeanList = this.lambdaQuery().eq(BlacklistBean::getType, type).list();
        if(CollectionUtils.isNotEmpty(blacklistBeanList)){
            return blacklistBeanList.stream().map(BlacklistBean::getSourceObjId).distinct().collect(Collectors.toList());
        }
        return null;
    }

    /**
     * 查询黑名单
     *
     * @param idNumberList ID
     * @param type     类型
     * @return 黑名单信息
     */
    @Override
    public List<String> findBlacklistByIdNumberInAndType(List<String> idNumberList, String type) {
        List<BlacklistBean> blacklistBeanList = this.lambdaQuery()
                .in(BlacklistBean::getIdNumber, idNumberList)
                .eq(BlacklistBean::getType, type).list();
        if(CollectionUtils.isNotEmpty(blacklistBeanList)){
            return blacklistBeanList.stream().map(BlacklistBean::getIdNumber).distinct().collect(Collectors.toList());
        }
        return null;
    }

    /**
     * 查询加入黑名单原因
     *
     * @param idNumber 证件
     * @param type     类型
     * @return 原因
     */
    @Override
    public String findReasonByIdNumberAndType(String idNumber, String type) {
        BlacklistBean one = this.lambdaQuery().eq(BlacklistBean::getIdNumber, idNumber)
                .eq(BlacklistBean::getType, type)
                .last(Constants.LAST_SQL).one();
        if(Objects.nonNull(one)){
            return one.getReason();
        }
        return null;
    }
}
